﻿
@{
    Layout = null;
}

<link href="~/libs/layui-v2.9.21/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />

<input  type="text" id="id" hidden="hidden" value="@ViewBag.id"/>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">仓库代码</label>
        <div class="layui-input-inline layui-input-wrap">
            <input type="text" id="warehouseCode" name="warehouseCode" lay-verify="required" autocomplete="off" lay-affix="clear" class="layui-input" placeholder="请输入仓库代码">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">仓库名称</label>
        <div class="layui-input-inline layui-input-wrap">
            <input type="text" id="warehouseName" name="warehouseName" lay-verify="required" autocomplete="off" lay-affix="clear" class="layui-input" placeholder="请输入仓库名称">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">仓库属性</label>
        <div class="layui-input-block" id="warehouseAttribute" >
            <input type="checkbox" name="warehouseAttribute" title="标准" value="标准">
            <input type="checkbox"  name="warehouseAttribute" title="简易" value="简易">
            <input type="checkbox"  name="warehouseAttribute" title="普通" value="普通">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="update">确定</button>
            <button type="reset" class="layui-btn layui-btn-primary" onclick="closeWorkStation()">关闭</button>
        </div>
    </div>
</form>

<!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.9.21/layui-v2.9.21/layui/layui.js"></script>
<script>
    //关闭弹框
    function closeWorkStation() {
        var index = parent.layer.getFrameIndex(window.name);//先得到当前的索引
        parent.layer.close(index);//再执行关闭
    }
layui.use(['form', 'laydate', 'util'], function(){
  var form = layui.form;
  var layer = layui.layer;
  var laydate = layui.laydate;
  var util = layui.util;


  // 提交事件
  form.on('submit(update)', function(data){
      var field = data.field; // 获取表单字段值
      //var warehouseAttributes = [];
      field = {
          id: $('#id').val(),
          warehouseCode: $('#warehouseCode').val(),
          warehouseName: $('#warehouseName').val(),
          //warehouseAttribute: $('input[name="warehouseAttribute"]:checked').each(function () {
          //    warehouseAttributes.push($(this).attr('title'));
          //}),
          warehouseAttribute: $('input[name="warehouseAttribute"]:checked').val(),
      }
    // 显示填写结果，仅作演示用
    layer.alert(JSON.stringify(field), {
      title: '当前填写的字段值'
    });
    // 此处可执行 Ajax 等操作
      $.ajax({
          type: 'post',
          url: '/Definition/UpdateDefinition',
          dataType: 'json',
          data: field,
          success: function (res) {
              if (res > 0) {
                  layer.msg('更新成功')
                  closeWorkStation()
                  return false;
              }
              if (res == -1) {
                  layer.msg('仓库编号已存在')
                  return false;
              }
          }
      })
    return false; // 阻止默认 form 跳转
  });

    $(document).ready(function () {
        getDefinitionById();
    })
    //反填
    function getDefinitionById() {
        var id = '@ViewBag.id';
        $.ajax({
            type: 'get',
            url:'/Definition/GetDefinitionById',
            dataType: 'json',
            data: { id: id },
            success: function (res) {
                $('#id').val(res.id);
                $('#warehouseCode').val(res.warehouseCode);
                $('#warehouseName').val(res.warehouseName);
                var warehouseAttributes = res.warehouseAttribute; // 假设返回的是对象数组，提取 warehouseAttribute 属性
                console.log(warehouseAttributes);
                var container = $('#warehouseAttribute');
                // 遍历多选框，根据仓库属性勾选
                container.find('input[type="checkbox"]').each(function (index, item) {
                    var warehouseAttribute = $(item).attr('title');
                    if (warehouseAttributes.includes(warehouseAttribute)) {
                        $(item).prop('checked', true);
                    } else {
                        $(item).prop('checked', false);
                    }
                });
                form.render('checkbox'); // 重新渲染多选框
            }
        })
    }
});
</script>

